package com.mkx.timerwheel;

import java.util.concurrent.TimeUnit;

/**
 * 默认的TimerTask实现类
 */
public class DefaultTimerTask implements TimerTask {
    private final Runnable task;
    private final long delayMs;
    private final long createTime;
    private long executionTime;
    
    /**
     * 构造函数
     * @param task 要执行的任务
     * @param delay 延迟时间
     * @param unit 时间单位
     */
    public DefaultTimerTask(Runnable task, long delay, TimeUnit unit) {
        if (task == null) {
            throw new IllegalArgumentException("Task cannot be null");
        }
        if (delay < 0) {
            throw new IllegalArgumentException("Delay cannot be negative");
        }
        this.task = task;
        this.delayMs = unit.toMillis(delay);
        this.createTime = System.currentTimeMillis();
        this.executionTime = createTime + delayMs;
    }
    
    @Override
    public void setExecutionTime(long executionTime) {
        this.executionTime = executionTime;
    }
    
    @Override
    public long getCreateTime() {
        return createTime;
    }
    
    @Override
    public long getExecutionTime() {
        return executionTime;
    }
    
    @Override
    public void run() {
        task.run();
    }
    
    @Override
    public String toString() {
        return "DefaultTimerTask{" +
                "delayMs=" + delayMs +
                ", createTime=" + createTime +
                ", executionTime=" + executionTime +
                ", remaining=" + (executionTime - System.currentTimeMillis()) + "ms}";
    }
}